package com.jd.ty.dao;

import java.util.List;

import org.hibernate.NonUniqueResultException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.jd.ty.util.Pager;


public class BaseDAO implements IBaseDAO{
    //private static Log log=LogFactory.getLog(BaseDAO.class);

    //  ********************************************************** //
    
	protected HibernateTemplate hbmTemplate;
//	private JdbcTemplate jdbcTemplate;
	
	@SuppressWarnings("unchecked")
	private Class entityType = null;
	
	
	public Object getById(long id) {
		return hbmTemplate.get(entityType, id);
	}
	
	public Object loadById(long id) {
		return hbmTemplate.load(entityType, id);
	}
	
	@SuppressWarnings("unchecked")
	public Object getOneByQuery(String filter, boolean unique) {
		List res = getByQuery(filter);
		if (res == null || res.isEmpty()) {
			return null;
		} else {
			if (unique && res.size() > 1) {
				throw new NonUniqueResultException(res.size());
			} else {
				return res.get(0);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public Object getOneByQuery(String filter, String sort, boolean unique) {
		List res = getByQuery(filter, sort);
		if (res == null || res.isEmpty()) {
			return null;
		} else {
			if (unique && res.size() > 1) {
				throw new NonUniqueResultException(res.size());
			} else {
				return res.get(0);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public List getByQuery(String filter) {
		return getByQuery(filter, null);
	}
	
	@SuppressWarnings("unchecked")
	public List getByQuery(String filter, String sort) {
		return getByQuery(null, filter, sort);
	}
	
	@SuppressWarnings("unchecked")
	public List getByQuery(Pager pager, String filter, String sort) {
		String hql = " from " + entityType.getName() + "  this ";

		if (filter != null && filter.length() != 0) {
			hql += " where " + filter;
		}
		
        Session session = hbmTemplate.getSessionFactory().getCurrentSession();
        if (pager != null) {
            //int c = hbmTemplate.find(hql).size();
        	Query countQuery = session.createQuery("select count(*) " + hql);
        	int c = ((Long) countQuery.uniqueResult()).intValue();
            pager.setRecords(c);
        }

        if (sort != null && sort.length() != 0) {
        	hql += " Order By " + sort;
        }
        
        Query query = session.createQuery(hql);
        if (pager != null) {
        	if (pager.getRecords() <= pager.getRow()*(pager.getPage()-1)) {
        		pager.setPage(1);
        	}
	        query.setFirstResult(pager.getFromRowIndex());
	        query.setMaxResults(pager.getRow());
        }
        //query.setResultTransformer(Criteria.ROOT_ENTITY);
        return query.list();
	}
	
    public long getCountByQuery(String filter) {
        String type = entityType.getName();
        String hql = " from " + type + " as  this ";
        if (filter != null && filter.length() != 0) {
        	hql += " where " + filter;
        }
        Session session = hbmTemplate.getSessionFactory().getCurrentSession();
    	Query countQuery = session.createQuery("select count(*) " + hql);
    	long c = (Long) countQuery.uniqueResult();

        return c;
    }

    public void add(Object obj) {
    	hbmTemplate.save(obj);
    }
        
    public void saveOrUpdate(Object obj) {
    	hbmTemplate.saveOrUpdate(obj);
	}
    public void merge(Object obj) {
    	hbmTemplate.merge(obj);
	}
    public void delete(Object obj) {
    	hbmTemplate.delete(obj);
    }

    public void flushSession(){
    	hbmTemplate.flush();
        //hbmTemplate.clear();
    }
    
    public void initialize(Object obj) {
    	hbmTemplate.initialize(obj);
    }
    

    public void setHbmTemplate(HibernateTemplate hbmTemplate) {
		this.hbmTemplate = hbmTemplate;
	}

//	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//		this.jdbcTemplate = jdbcTemplate;
//	}


//	public JdbcTemplate getJdbcTemplate() {
//		return jdbcTemplate;
//	}
    
	@SuppressWarnings("unchecked")
	protected void setEntityType(Class entityType) {
		this.entityType = entityType;
	}

	public void setDaoTemplate(Object template) {
		this.hbmTemplate = (HibernateTemplate)template;
		
	}


}

